﻿using System;
using System.Web;
using System.IO;
using System.Drawing;
using Newtonsoft.Json;
using SourceAFIS.Simple;

namespace FingerprintService
{
    /// <summary>
    /// FpHandler 的摘要说明
    /// </summary>
    public class FpHandler : IHttpHandler
    {
        private AfisEngine afis = new AfisEngine();              //指纹引擎

        public void ProcessRequest(HttpContext context)
        {
            String method = context.Request.Params["method"];

            switch (method)
            {
                case "extract": //提取摸版
                    extract(context);
                    break;
                case "verify":  //计算分数
                    break;
            }
        }

        /// <summary>
        /// 提取指纹摸版
        /// </summary>
        /// <param name="context">上下文</param>
        private void extract(HttpContext context)
        {
            String type = context.Request.Params["type"] == null ? "iso" : context.Request.Params["type"].ToLower().Trim(); //指纹类型。 可选指：iso、xml

            string json = "";
            string data = null;

            HttpFileCollection files = context.Request.Files;
            if (files.Count == 0)
            {
                json = JsonConvert.SerializeObject(new ResultObject("400", "请上传文件"));
            }
            else
            {
                try
                {
                    Fingerprint fp = ExtractFingerprint(files[0].InputStream);
           
                    //按类型获取指纹模板
                    if (type == "xml")
                    {
                        data = fp.AsXmlTemplate.ToString();
                    }
                    else
                    {
                        data = Convert.ToBase64String(fp.AsIsoTemplate);
                    }

                    json = JsonConvert.SerializeObject(new ResultObject("200", "提取成功", data));
                }
                catch (Exception err)
                {
                    json = JsonConvert.SerializeObject(new ResultObject("400", "提取失败", err.Message));
                }

            }

            context.Response.ContentType = "application/json; charset=utf-8";
            context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
            context.Response.Write(json);
            context.Response.End();
        }

        /// <summary>
        /// 提取指纹对象
        /// </summary>
        /// <param name="stream">流</param>
        /// <returns>指纹对象</returns>
        private Fingerprint ExtractFingerprint(Stream stream)
        {
            //创建指纹对象
            Fingerprint fp = new Fingerprint();
            using (Bitmap bitmap = new Bitmap(stream))
            {
                fp.AsBitmap = bitmap;
            }
            
            //创建人对象
            Person person = new Person();
            person.Fingerprints.Add(fp);    //加入指纹

            //提取指纹摸版
            afis.Extract(person);

            return fp;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}